<?php
namespace app\api\controller\v1;
use Thenbsp\Wechat\Payment\Unifiedorder;
use Thenbsp\Wechat\Payment\Qrcode\Temporary;
use think\Db;
use think\facade\{
    Env
};
class Publics extends Base
{
	//用户手机短信登陆
    public function smslogin()
    {
        $d = request()->post();
        //验证图形验证码
        if(!model('ComCodeimg')->checkCode($d['img_token'],$d['img_code'])){
            return err(model('ComCodeimg')->getError());
        }
        //验证短信验证码
        if(!model('ComCodephone')->checkCode($d['phone'],$d['phone_token'],$d['phone_code'])){
            return err(model('ComCodephone')->getError());
        }
        //执行登陆
        $user = model('user');
    	$token = $user->login($d['phone']);
    	if($token)
        	return suc(['token'=>$token]);
        return err($user->getError());
    }
    //用户手机短信找回密码
    public function smseditpwd()
    {
        $d = request()->post();
        if(!model('ComCodeimg')->checkCode($d['img_token'],$d['img_code'])){
            return err(model('ComCodeimg')->getError());
        }
        if(!model('ComCodephone')->checkCode($d['phone'],$d['phone_token'],$d['phone_code'])){
            return err(model('ComCodephone')->getError());
        }
        $rs = model('ComStepbystep')::create(['phone'=>$d['phone'],'type'=>0]);
        return suc(['type'=>$rs->type,'phone'=>$rs->phone,'step_token'=>$rs->token]);
    }

    //重置密码
    public function editpwd(){
        $d = request()->post();
        $rule = [
              'password'=>'require|integer',
              'repassword'=>'require|integer',
        ];
        $msg = [];
        $r = $this->validate($d, $rule,$msg);
        if($r!==true) return err($r);

        if(!model('ComStepbystep')->checkToken($d['phone'],$d['step_token'],$d['type'])){
            return err(model('ComStepbystep')->getError());
        }
        $rs = model('user')->editpwd($d['phone'],$d['password']);
        if($rs){
            return suc();
        }else{
            return error($rs->getError());
        }
    }
}
